import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:god_logistics/pages/home/taskPage.dart';
import 'package:god_logistics/pages/loginVIew/loginPage.dart';
import 'package:god_logistics/pages/mine/minePage.dart';
import 'package:god_logistics/pages/news/newsPage.dart';
import 'package:god_logistics/utils/eventbus.dart';
import 'package:god_logistics/utils/showToast.dart';
import 'package:god_logistics/utils/tokenManage.dart';

class tabpage extends StatefulWidget {
  const tabpage({super.key});

  @override
  State<tabpage> createState() => _tabpageState();
}

class _tabpageState extends State<tabpage> {
  int currentIndex = 0;
  // 底部TabBar数据
  List tabBarList = [
    {"title": "任务", "default": "assets/icons/ic_tab_btn_task.svg"},
    {"title": "消息", "default": "assets/icons/ic_tab_btn_mess_nor.svg"},
    {"title": "我的", "default": "assets/icons/ic_tab_btn_mine_nor.svg"},
  ];

  String tokenis = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    eventBus.on<LogoutEvent>().listen((event) {
      // 跳转登录页面
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // 1.2 主体部分
      body: IndexedStack(
        index: currentIndex,
        children: [
          Taskpage(),
          Newspage(),
          Minepage(currentIndex: currentIndex),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: barItemList(),
        type: BottomNavigationBarType.fixed,
        selectedItemColor: Color.fromARGB(255, 231, 63, 63),
        currentIndex: currentIndex,
        unselectedItemColor: Colors.black,
        onTap: (index) {
          setState(() {
            currentIndex = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> list = [];
    for (int i = 0; i < tabBarList.length; i++) {
      list.add(
        BottomNavigationBarItem(
          icon: SvgPicture.asset(tabBarList[i]["default"]),
          activeIcon: SvgPicture.asset(
            tabBarList[i]["default"],
            colorFilter: ColorFilter.mode(
              Colors.red, // 想要的颜色
              BlendMode.srcIn, // 混合模式，srcIn通常用于替换颜色
            ),
            width: 40,
            height: 40,
          ),
          label: tabBarList[i]["title"],
        ),
      );
    }
    return list;
  }
}
